package com.adminpanel.action;



import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.adminpanel.modelbean.ProductBean;
import com.adminpanel.modelbean.ProductTypeBean;
import com.adminpanel.modelbean.UserBean;
import com.adminpanel.service.ProductService;
import com.adminpanel.service.UserfileServices;


@RequestMapping("/myinfo")
@Controller
@SessionAttributes("loginuser")
public class StylesController {
	
	@Resource(name = "productServices")
	private ProductService productService;
	
	@Resource(name = "userfileServices")
	private UserfileServices userfileServices;
	
	
	@RequestMapping("mystyles")
	public String StylesIndex() {

		return "styles";

	}
	
	
	@RequestMapping("myfiles")
	public String StylesFiles(@ModelAttribute("loginuser") UserBean userBean,Model model) {
		model.addAttribute("uf", userfileServices.getFileByUserId(userBean.getId()));
		//model.addAttribute("usercountfiles", userfileServices.getUserfileCounts(userBean.getId()));
		return "userfile";

	}
	
//	@RequestMapping("ufiles")
//	public String getUserfiles(@ModelAttribute("loginuser") UserBean userBean,Model model){
//		model.addAttribute("uf", userfileServices.getFileByUserId(userBean.getId()));
//		return "ufiles";
//	}
	
	
	@RequestMapping("addproduct")
	public String Addproduct() {

		return "addproduct";

	}
	
	
	@RequestMapping(value="showproduct")
	public String getproductinfo(@RequestParam("id") int id,ModelMap modelMap){
		modelMap.addAttribute("productinfo", productService.getProductDetailById(id));
		return "showproduct";
	}
	
	
	@RequestMapping(value="checkProductId",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkSameProductId(HttpServletResponse response,
			HttpServletRequest request) {
		String inputProductid = request.getParameter("productid");
		int matchcount = productService.findExistProductID(inputProductid);
		if (matchcount == 0) {

			return true;
		}

		return false;

	}
	
	
	@RequestMapping(value="checkProductName",method = RequestMethod.POST)
	@ResponseBody
	public boolean checkSameProductName(HttpServletResponse response,
			HttpServletRequest request) {
		String inputProductName = request.getParameter("productname");
		int matchcount = productService.findExistProductName(inputProductName);
		if (matchcount == 0) {

			return true;
		}

		return false;

	}
	
	@RequestMapping(value="myproduct",method = RequestMethod.GET)
	public String product(@ModelAttribute("loginuser") UserBean userBean,Model model){
		//System.out.println(id);
		model.addAttribute("products", productService.getProductByUserId(userBean.getId()));
		return "myproduct";
	}
	
	@RequestMapping(value="saveproduct" ,method = RequestMethod.POST)
	public String save_product(@ModelAttribute("loginuser") UserBean userBean,ProductBean productBean,HttpServletRequest request,ModelMap map){
		productBean.setUser_id(userBean.getId());
		productBean.setProduct_id(request.getParameter("productid"));
		productBean.setProduct_name(request.getParameter("productname"));
		productBean.setProduct_describe(request.getParameter("productdesc"));
		productBean.setProduct_type(request.getParameter("productcat"));
		productBean.setProduct_vendor(request.getParameter("productvendor"));
		productBean.setIsDraft(0);
		productService.addProduct(productBean);
		return "redirect:myproduct";
		
	}
	
	@RequestMapping(value="updateproduct",method = RequestMethod.POST)
	public String update_product(@RequestParam("id") int id,HttpServletRequest request){
		productService.updateProduct(request.getParameter("productdesc1"),request.getParameter("productcat1"),id);
		return "redirect:myproduct";
	}
	
	@RequestMapping(value="delproduct", method = RequestMethod.POST)
	public void del_product(@RequestParam("delid") String params){
		productService.delProduct(params);
	}
	
	@RequestMapping("getpt")
	@ResponseBody
	public ModelMap getpt(){
		//model.addAttribute("mypts", productService.getProductType());
		ModelMap modelMap = new ModelMap();
		modelMap.addAttribute("mypts", productService.getProductType());
		return modelMap; 
		//return "addproduct";
	}
	
	@RequestMapping("addpt")
	public String addpt(HttpServletRequest request,ProductTypeBean productTypeBean){
		productTypeBean.setType(request.getParameter("producttype"));
		productService.addProductType(productTypeBean);
		return null;
		
	}
	
	
	

}
